import myAjax from '../static/myAjax.js'

window.onload = function () {


    // 退出登录
    if (document.getElementById('logout')) {
        document.getElementById('logout').onclick = function () {
            myAjax('/backstage/logout', 'GET')
                .then(
                    res => {
                        if (res.code == 200) {
                            sessionStorage.removeItem("userName");
                            location.href = res.url;
                        }
                    }
                )
        }
    }

    //添加权限
    if (document.getElementById('sureAdd')) {
        document.getElementById('sureAdd').onclick = function () {
            let authorityVal = document.getElementById('authority-name').value,
                urlVal = document.getElementById('url-name').value,
                parentId = document.getElementById('parent-name').value;

            myAjax('/authority/add', 'POST', 'authorityVal=' + authorityVal + '&urlVal=' + urlVal + '&parentId=' + parentId)
                .then(
                    res => {
                        if (res.code == 200) {
                            alert('添加成功');
                            document.getElementById('exampleModal').style.display = 'none';
                        }
                        location.reload();
                    }
                )

        }
    }

    //删除权限
    if (document.querySelectorAll('#delAuthority')) {
        document.querySelectorAll('#delAuthority').forEach(item => {
            item.onclick = function () {
                // console.log(item.parentElement.parentElement.children[0].textContent);
                let authority = item.parentElement.parentElement.children[0].textContent;
                // console.log(authority);
                myAjax('/authority/delete', 'POST', 'authority=' + authority)
                    .then(
                        res => {
                            if (confirm('确定要删除吗？')) {
                                alert('删除成功')
                                location.reload();
                            }

                        }
                    )
            }
        })
    }

    //添加角色（管理员）
    if (document.getElementById('sureAddRole')) {
        document.getElementById('sureAddRole').onclick = function () {
            let roleVal = document.getElementById('role-name').value;

            myAjax('/role/addRole', 'POST', 'roleVal=' + roleVal)
                .then(
                    res => {
                        if (res.code == 200) {
                            alert('添加成功');
                            document.getElementById('exampleModal').style.display = 'none';
                        }
                        location.reload();
                    }
                )
        }
    }

    //添加课程
    if (document.getElementById('sureAddCourse')) {
        document.getElementById('sureAddCourse').onclick = function () {
            let courseVal = document.getElementById('course-name').value,
                typeVal = document.getElementById('type-name').value;

            myAjax('/course/addCourse', 'POST', 'courseVal=' + courseVal + '&typeVal=' + typeVal)
                .then(
                    res => {
                        console.log(res);
                        if (res.code == 200) {
                            alert('添加成功');
                            document.getElementById('exampleModal').style.display = 'none';
                        }
                        location.reload();
                    }
                )
        }
    }

    //删除课程
    if (document.querySelectorAll('#delCourse')) {
        document.querySelectorAll('#delCourse').forEach(item => {
            item.onclick = function () {
                let course = item.parentElement.parentElement.children[0].textContent;
                // // console.log(authority);
                myAjax('/course/delete', 'POST', 'course=' + course)
                    .then(
                        res => {
                            if (confirm('确定要删除吗？')) {
                                alert('删除成功')
                                location.reload();
                            }

                        }
                    )
            }
        })
    }

    layui.use(['element', 'layer', 'util'], function () {
        var element = layui.element
            , layer = layui.layer
            , util = layui.util
            , $ = layui.$;

        //头部事件
        util.event('lay-header-event', {
            //左侧菜单事件
            menuLeft: function (othis) {
                layer.msg('展开左侧菜单的操作', { icon: 0 });
            }
            , menuRight: function () {
                layer.open({
                    type: 1
                    , content: '<div style="padding: 15px;">处理右侧面板的操作</div>'
                    , area: ['260px', '100%']
                    , offset: 'rt' //右上角
                    , anim: 5
                    , shadeClose: true
                });
            }
        });

    });
}